package com.vote.www.service;

import com.vote.www.domain.Czjuserapprove;
import com.vote.www.domain.Czjuserinfo;
import com.vote.www.domain.Czjusermess;
import com.vote.www.persistence.CzjuserapproveMapper;
import com.vote.www.utils.BeanToMapUtil;
import com.vote.www.utils.ConvertObj;
import com.vote.www.utils.UUIDUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.stereotype.Service;

import tangwj.person.dao.ResourceDao;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 汤伟杰 on 2017/5/6.
 */
@Service
public class CzjuserapproveService {
    @Autowired
    private CzjuserapproveMapper czjuserapproveMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private ResourceDao resourceDao;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void update(Czjuserapprove czjuserapprove){
        czjuserapprove.setModifytime(new Date());
        czjuserapproveMapper.update(czjuserapprove);
    }

    public void updateApproveStatus(int approveStatus,String userapproveId){
        if(userapproveId!=null){
            jdbcTemplate.update("update czj_userapprove set approveStatus='" + approveStatus + "',approvetime='"+sdf.format(new Date())
                    +"',isuse='0' where userapproveId='" + userapproveId + "'");
        }
    }
    public void insert(Czjuserapprove czjuserapprove) throws Exception {
        czjuserapprove.setApproveStatus(0);
        czjuserapprove.setIsuse(0);
        czjuserapprove.setCreatetime(new Date());
        czjuserapprove.setModifytime(new Date());
        czjuserapprove.setUserapproveId(UUIDUtils.getUUID());
        resourceDao.insert(czjuserapprove);
    }
    public void updateUse(int use,String userapproveId) throws Exception {
        if(userapproveId!=null){
            jdbcTemplate.update("update czj_userapprove set isuse='" + use + "' where userapproveId='" + userapproveId + "'");
        }
    }
    public void aotoUpdate(int userinfoId) throws Exception {
        Czjuserapprove czjuserapprove = null;
        System.out.println(userinfoId);
        int count = jdbcTemplate.queryForObject("select count(*) from czj_userapprove where userinfoId='"+userinfoId+"' order by createtime desc limit 1",int.class);
        if(count!=0){
            czjuserapprove = (Czjuserapprove) jdbcTemplate.queryForObject("select * from czj_userapprove where userinfoId='"+userinfoId+"' order by createtime desc limit 1",new BeanPropertyRowMapper(Czjuserapprove.class));
            if(czjuserapprove.getApproveStatus()==1){
                czjuserapprove = null;
            }
        }



        Czjuserinfo czjuserinfo = (Czjuserinfo) jdbcTemplate.queryForObject("select * from czj_userinfo where id='"+userinfoId+"'",new BeanPropertyRowMapper(Czjuserinfo.class));

        Czjusermess czjusermess = (Czjusermess) jdbcTemplate.queryForObject("select * from czj_usermess where userinfoId='" + userinfoId + "'",new BeanPropertyRowMapper(Czjusermess.class));

        if(czjuserapprove==null){
            czjuserapprove = new Czjuserapprove();
            czjuserapprove.setUserinfoId(userinfoId);

            copyDate(czjuserapprove,czjuserinfo,czjusermess);
            insert(czjuserapprove);
        }else{
            copyDate(czjuserapprove,czjuserinfo,czjusermess);
            update(czjuserapprove);
        }



    }

    private void copyDate(Czjuserapprove czjuserapprove, Czjuserinfo czjuserinfo, Czjusermess czjusermess) {
        czjuserapprove.setName(czjusermess.getFullName());
        czjuserapprove.setNick(czjusermess.getNick());
        czjuserapprove.setTelephone(czjuserinfo.getTelephone());
        czjuserapprove.setSex(czjusermess.getSex());
        czjuserapprove.setIdCard(czjusermess.getIdCard());
        czjuserapprove.setPicPath(czjusermess.getPicPath());
        czjuserapprove.setLocus(czjusermess.getLocus());
        czjuserapprove.setIrectSeeding(czjusermess.getIrectSeeding());
        czjuserapprove.setUusee(czjusermess.getUusee());
        czjuserapprove.setFans(czjusermess.getFans());

//        czjuserapprove.setAge(idcardToAge(czjuserapprove.getIdCard()));
        czjuserapprove.setRepresent(czjusermess.getRepresent());
        czjuserapprove.setContent(czjusermess.getContent());
        czjuserapprove.setExt2(czjusermess.getExt2());
        czjuserapprove.setVodeoUri(czjusermess.getVodeoUri());
        czjuserapprove.setApproveStatus(0);
    }

    public Integer idcardToAge(String idCard) {
        try {
            if(idCard.length()!=18){
                return 0;
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMdd");
            String nyr = idCard.substring(6,14);
            String year = nyr.substring(0,4);
            String monthdate = nyr.substring(4,8);
            String _nyr = sdf.format(new Date());
            String[] _nyrs = _nyr.split("-");
            int age=0;
            if(Integer.parseInt(_nyrs[1])>=Integer.parseInt(monthdate)){
                age = Integer.parseInt(_nyrs[0])-Integer.parseInt(year);
            }else{
                age = Integer.parseInt(_nyrs[0])-Integer.parseInt(year)-1;
            }
            return age;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }


    public List<Czjuserapprove> getUserapproveList(String id) throws InvocationTargetException, IntrospectionException, InstantiationException, IllegalAccessException {
        List<Map<String,Object>> list = jdbcTemplate.queryForList("select * from czj_userapprove where userinfoId='"+id+"' order by createtime desc");
        List<Czjuserapprove> lt = new ArrayList<Czjuserapprove>();
        for (Map<String, Object> map : list) {
            lt.add((Czjuserapprove) BeanToMapUtil.convertMap(Czjuserapprove.class, map));
        }
        return lt;
    }

    public Map<String,Object> isuse(int status, String id) {
        Czjuserapprove entity = getById(id);
        Map<String,Object> map = new HashMap<String,Object>();
        if(status==1){
            if(entity.getApproveStatus()==1){
                map.put("status",1);
                    jdbcTemplate.update("update czj_userapprove set isuse='0' where userinfoId='"+entity.getUserinfoId()+"'");
                    jdbcTemplate.update("update czj_userapprove set isuse='1' where userapproveId='"+id+"'");



            }else{
                map.put("status",0);
                map.put("msg","审批通过的数据才能设为启用！");
            }
        }else{
            map.put("status",1);
            jdbcTemplate.update("update czj_userapprove set isuse='0' where userapproveId='"+id+"'");
        }
        return map;

    }

    public Czjuserapprove getById(String id){
        return (Czjuserapprove) jdbcTemplate.queryForObject("select * from czj_userapprove where userapproveId='"+id+"'",new BeanPropertyRowMapper(Czjuserapprove.class));
    }
	public Czjuserapprove getNew(Integer userinfoId) {
	
		try {
			return (Czjuserapprove) jdbcTemplate.queryForObject("select * from czj_userapprove where userinfoId='"+userinfoId+"' order by createtime desc limit 1",new BeanPropertyRowMapper(Czjuserapprove.class));
		} catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
         
	}

	public void updateUrl(String userapproveId, String vodeoUri) {
		
		   if(userapproveId!=null){
	            jdbcTemplate.update("update czj_userapprove set vodeoUri='" + vodeoUri + "' where userapproveId='" + userapproveId + "'");
	        }
		
	}
}
